#include "webtabwidget.h"
#include "mainwnd.h"

#define TABWIDGET_CSS "QTabBar::tab {\
height: 32px;\
width: 240px;\
padding-left: 2px;\
padding-right: 2px;\
border-right: 1px solid gray;\
}\
QTabBar::tab:selected, QTabBar::tab:hover {\
background: white;\
border-style: solid;\
border-color: white;\
border-right: 1px solid gray;\
}"

WebTabWidget::WebTabWidget(QWidget *parent)
    : QTabWidget(parent)
{
    m_tabButton = nullptr;
    m_menuTab = nullptr;
    m_menuBlank = nullptr;
    m_hitIndex = -1;
    m_tabIcon = QIcon(":/pic/browser.svg");
    m_lastIndex = -1;

    //
    init();
}

WebTabWidget::~WebTabWidget()
{

}

void WebTabWidget::init()
{
    this->setDocumentMode(true);
    this->setElideMode(Qt::ElideRight);
    this->setStyleSheet(TABWIDGET_CSS);

    //tabBar
    QTabBar * tabBar = this->tabBar();
    tabBar->setExpanding(false);
    tabBar->setTabsClosable(true);
    tabBar->setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
    tabBar->setMovable(true);
    tabBar->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(tabBar, &QTabBar::customContextMenuRequested, this, &WebTabWidget::onTabContextMenuRequested);
    connect(tabBar, &QTabBar::currentChanged, this, &WebTabWidget::onCurrentChanged);
    connect(tabBar, &QTabBar::tabCloseRequested, this, &WebTabWidget::onTabCloseRequested);
    connect(tabBar, &QTabBar::tabBarDoubleClicked, this, &WebTabWidget::onTabDoubleClicked);

    //right
    m_tabButton = new WebTabButton(this);
    m_tabButton->setObjectName("m_tabButton");
    m_tabButton->setTabWidget(this);

    connect(m_tabButton, &WebTabButton::btNewClicked, this, &WebTabWidget::btNewClicked);
    connect(m_tabButton, &WebTabButton::btMenuClicked, this, &WebTabWidget::btMenuClicked);
    connect(m_tabButton, &WebTabButton::btMinClicked, this, &WebTabWidget::btMinClicked);
    connect(m_tabButton, &WebTabButton::btMaxClicked, this, &WebTabWidget::btMaxClicked);
    connect(m_tabButton, &WebTabButton::btCloseClicked, this, &WebTabWidget::btCloseClicked);

    this->setCornerWidget(m_tabButton, Qt::TopRightCorner);

    //
    initMenu();
}

void WebTabWidget::initMenu()
{
    //menu
    QAction * atNewTab = new QAction(tr("新建标签"));
    connect(atNewTab, &QAction::triggered, this, &WebTabWidget::onNewTab);

    QAction * atCloneTab = new QAction(tr("复制标签"));
    connect(atCloneTab, &QAction::triggered, this, &WebTabWidget::onCloneTab);

    QAction * atCloseTab = new QAction(tr("关闭标签"));
    connect(atCloseTab, &QAction::triggered, this, &WebTabWidget::onCloseTab);

    QAction * atCloseOtherTab = new QAction(tr("关闭其它标签"));
    connect(atCloseOtherTab, &QAction::triggered, this, &WebTabWidget::onCloseOtherTab);

    QAction * atCloseAllTab = new QAction(tr("关闭所有标签"));
    connect(atCloseAllTab, &QAction::triggered, this, &WebTabWidget::onCloseAllTab);

    QAction * atReloadTab = new QAction(tr("重新加载"));
    connect(atReloadTab, &QAction::triggered, this, &WebTabWidget::onReloadTab);

    QAction * atReloadOtherTab = new QAction(tr("重新加载其他"));
    connect(atReloadOtherTab, &QAction::triggered, this, &WebTabWidget::onReloadOtherTab);

    QAction * atReloadAllTab = new QAction(tr("重新加载所有"));
    connect(atReloadAllTab, &QAction::triggered, this, &WebTabWidget::onReloadAllTab);

    m_menuTab = new QMenu(this);
    Q_ASSERT(m_menuTab != nullptr);

    m_menuTab->addAction(atNewTab);
    m_menuTab->addAction(atCloneTab);
    m_menuTab->addSeparator();
    m_menuTab->addAction(atCloseTab);
    m_menuTab->addAction(atCloseOtherTab);
    m_menuTab->addAction(atCloseAllTab);
    m_menuTab->addSeparator();
    m_menuTab->addAction(atReloadTab);
    m_menuTab->addAction(atReloadOtherTab);
    m_menuTab->addAction(atReloadAllTab);

    m_menuBlank = new QMenu(this);
    Q_ASSERT(m_menuBlank != nullptr);

    m_menuBlank->addAction(atNewTab);
    m_menuBlank->addAction(atCloseAllTab);
    m_menuBlank->addAction(atReloadAllTab);
}

QWidget* WebTabWidget::getButtonWidget()
{
    return m_tabButton;
}

QWidget* WebTabWidget::getNewWidget()
{
    return m_tabButton->getNewWidget();
}

QWidget* WebTabWidget::getTitleWidget()
{
    return m_tabButton->getTitleWidget();
}

QWidget* WebTabWidget::getMenuWidget()
{
    return m_tabButton->getMenuWidget();
}

QWidget* WebTabWidget::getMinWidget()
{
    return m_tabButton->getMinWidget();
}

QWidget* WebTabWidget::getMaxWidget()
{
    return m_tabButton->getMaxWidget();
}

QWidget* WebTabWidget::getCloseWidget()
{
    return m_tabButton->getCloseWidget();
}


WebWnd * WebTabWidget::newTab(const QString & strUrl, bool current, int index)
{
    WebWnd * wnd = new WebWnd(strUrl);

    addTab(wnd, current, index);

    return wnd;
}


void WebTabWidget::onNewWebWnd(QString strUrl, bool current)
{
    WebWnd * wnd = new WebWnd(strUrl);

    addTab(wnd, current, -1);
}


int WebTabWidget::addTab(WebWnd *wnd, bool current, int index)
{
    //
    connect(wnd, &WebWnd::newWebWnd, this, &WebTabWidget::onNewWebWnd);
    connect(wnd, &WebWnd::newMainWnd, this, &WebTabWidget::newMainWnd);

    connect(wnd, &WebWnd::loadIconChange, this, &WebTabWidget::onWebIconChanged);
    connect(wnd, &WebWnd::titleChange, this, &WebTabWidget::onWebTitleChanged);

    //
    int newIndex = this->insertTab(index, wnd, m_tabIcon, "新标签页");
    if (current)
    {
        this->setCurrentIndex(newIndex);
    }

    return newIndex;
}

WebWnd * WebTabWidget::cloneTab(int index)
{
    if (index < 0 || index > count())
    {
        return nullptr;
    }

    WebWnd * old = webWnd(index);
    if (old == nullptr)
    {
        return nullptr;
    }

    return newTab(old->getUrl());
}

int WebTabWidget::closeTab(int index)
{
    if (index < 0 || index > count())
    {
        return -1;
    }

    QWidget * wg = this->widget(index);

    this->removeTab(index);

    if (wg != nullptr)
    {
        //关闭tab页时必须与父窗口断开，否则会向主窗口发送关闭消息
        wg->setParent(nullptr);
        wg->close();

        delete wg;
        wg = nullptr;
    }

    if (count() <= 0)
    {
        emit allTabClosed();
    }

    return 0;
}

int WebTabWidget::closeOtherTab(int index)
{
    if (index < 0 || index > count())
    {
        return -1;
    }

    for (int i = count() - 1; i > index; --i)
    {
        closeTab(i);
    }

    for (int i = index - 1; i >= 0; --i)
    {
        closeTab(i);
    }

    return 0;
}

int WebTabWidget::closeAllTab()
{
    for (int i = count() - 1; i >= 0; --i)
    {
        closeTab(i);
    }

    return 0;
}

int WebTabWidget::reloadTab(int index)
{
    if (index < 0 || index > count())
    {
        return -1;
    }

    webWnd(index)->reload();

    return 0;
}

int WebTabWidget::reloadOtherTab(int index)
{
    if (index < 0 || index > count())
    {
        return -1;
    }

    for (int i = count() - 1; i > index; --i)
    {
        reloadTab(i);
    }

    for (int i = index - 1; i >= 0; --i)
    {
        reloadTab(i);
    }

    return 0;
}

int WebTabWidget::reloadAllTab()
{
    for (int i = count() - 1; i >= 0; --i)
    {
        reloadTab(i);
    }

    return 0;
}

void WebTabWidget::onNewTab()
{
    newTab();
}

void WebTabWidget::onCloneTab()
{
    int index = m_hitIndex;
    if (index < 0 || index > count())
    {
        index = currentIndex();
    }

    cloneTab(index);
}

void WebTabWidget::onCloseTab()
{
    int index = m_hitIndex;
    if (index < 0 || index > count())
    {
        index = currentIndex();
    }

    closeTab(index);
}

void WebTabWidget::onCloseOtherTab()
{
    int index = m_hitIndex;
    if (index < 0 || index > count())
    {
        index = currentIndex();
    }

    closeOtherTab(index);
}

void WebTabWidget::onCloseAllTab()
{
    closeAllTab();
}

void WebTabWidget::onReloadTab()
{
    int index = m_hitIndex;
    if (index < 0 || index > count())
    {
        index = currentIndex();
    }

    reloadTab(index);
}

void WebTabWidget::onReloadOtherTab()
{
    int index = m_hitIndex;
    if (index < 0 || index > count())
    {
        index = currentIndex();
    }

    reloadOtherTab(index);
}

void WebTabWidget::onReloadAllTab()
{
    reloadAllTab();
}

void WebTabWidget::onTabContextMenuRequested(const QPoint & pos)
{
    m_hitIndex = this->tabBar()->tabAt(pos);

    if (m_hitIndex >= 0)
    {
        m_menuTab->exec(QCursor::pos());
    }
    else
    {
        m_menuBlank->exec(QCursor::pos());
    }
}

void WebTabWidget::onTabCloseRequested(int index)
{
    closeTab(index);
}

void WebTabWidget::onTabDoubleClicked(int index)
{
    if (index < 0)
    {
        onNewTab();
    }
}

void WebTabWidget::onCurrentChanged(int index)
{
    //
    m_lastIndex = index;
}

void WebTabWidget::onWebIconChanged(const QIcon & icon)
{
    this->setTabIcon(this->indexOf(qobject_cast<WebWnd*>(this->sender())), icon);
}

void WebTabWidget::onWebTitleChanged(const QString &title)
{    
    this->setTabText(this->indexOf(qobject_cast<WebWnd*>(this->sender())), title);
}


QWidget * WebTabWidget::topParentWidget()
{
    QWidget * wnd = parentWidget();

    while (wnd != nullptr && wnd->parentWidget() != nullptr)
    {
        wnd = wnd->parentWidget();
    }

    return wnd;
}
